package com.example.mapping.utils;

import com.example.mapping.constant.Constants;
import com.google.common.collect.Lists;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

import static com.example.mapping.constant.Constants.MappingConstants.MAPPINGSPECDELIMITER;

/**
 * 公用工具类
 *
 * @author baoyoujia
 */
public class CommonUtils {

    /**
     * long集合转字符串
     *
     * @param list      long集合
     * @param delimiter 分隔符
     * @return 字符串
     */
    public static String list2Str(List<Long> list, String delimiter) {
        if (CollectionUtils.isEmpty(list)) {
            return Constants.Symbol.EMPTY;
        }
        return list.stream().map(String::valueOf).collect(Collectors.joining(delimiter));
    }

    /**
     * 将字符串转换成list
     *
     * @param str string集合
     * @return
     */
    public static List<String> str2List(String str, String delimiter) {
        if (StringUtils.isEmpty(str)) {
            return Collections.emptyList();
        }
        return Arrays.asList(str.split(delimiter));
    }

    /**
     * 将json转换成queue
     * @param mappingAbsolute 映射路径
     * @return 队列
     */
    public static Queue<String> mappingAbsoluteQueue(String mappingAbsolute) {
        if (StringUtils.isEmpty(mappingAbsolute)) {
            return Lists.newLinkedList();
        }
        return Arrays.stream(mappingAbsolute.split(MAPPINGSPECDELIMITER)).collect(Collectors.toCollection(LinkedList::new));
    }
}
